#include "projectile.h"
#include "global.h"

#define xcenter SCREEN_W/2
#define ycenter SCREEN_H/2
#define GREEN makecol(0, 255, 0)
#define RED makecol(255, 0, 0)
#define BLUE makecol(0, 0, 255)
#define YELLOW makecol(255, 255, 0)
#define PURPLE makecol(128, 0, 255)
#define WHITE makecol(255, 255, 255)

extern int score;
extern int max_asteroids;

void spread_shot(ship*, projectile* spread[]);
void single_shot(ship, projectile* bullet);
void laser_shot(ship*, projectile* laser);

projectile::projectile(int x, int y, int s, float t, int d, int tp){
    this->power = 1;
    this->exists = false;
    this->x = x;
    this->y = y;
    this->speed = s;
    this->theta = t;
    this->distance = d;
    this->size = 1;
    this->type = tp;
}

//---------------------------------------------------------------------------------------------------------
void projectile::move(asteroid* roids[], powerup* pow){
    if(bullet_fired){
        this->x = int((this->distance += this->speed) * (cos(this->theta)) + xcenter);
        this->y = int((this->distance += this->speed) * (sin(this->theta)) + ycenter);

        if (this->x < (SCREEN_W*.02) + 1 || this->x > SCREEN_W - (SCREEN_W*.02) -1 || this->y < (SCREEN_H*.05)+1 || this->y > SCREEN_H - (SCREEN_H*.02)-1){
            bullet_fired -= 1;
            this->exists = false;
            if(score >= 5){
                score -= 5;
            }
            else score = 0;
        }
        for(int i = 0; i < max_asteroids; i++){
            if(this->type == 0 || this->type == 1){
                if ((this->x + this->size - 1) > (roids[i]->get_x() - roids[i]->get_size()) &&
                    (this->x - this->size + 1) < (roids[i]->get_x() + roids[i]->get_size()) &&
                    (this->y + this->size - 1) > (roids[i]->get_y() - roids[i]->get_size()) &&
                    (this->y - this->size + 1) < (roids[i]->get_y() + roids[i]->get_size())  ) {
                    bullet_fired -= 1;
                    play_sample(hit_roid, 255, 128, 1000, 0);
                    this->exists = false;

                    roids[i]->set_power(roids[i]->get_power() - this->power);

                    if(roids[i]->get_power() <= 0) {                                                             //ASTEROID DESTROYED BY BULLET
                        if(roids[i]->get_powerup()){
                            pow->set_exists(true);
                            pow->set_type       (roids[i]->get_powerup());
                            pow->set_x          (roids[i]->get_x());
                            pow->set_y          (roids[i]->get_y());
                            pow->set_xtheta     (roids[i]->get_xtheta());
                            pow->set_ytheta     (roids[i]->get_ytheta());
                            pow->set_speed      (roids[i]->get_speed());
                            pow->set_distance   (roids[i]->get_distance());
                        }
                        roids[i]->set_powerup(0);
                        play_sample(splode_sound, 255, 128, 1000, 0);
                        explode_gfx(this->x, this->y, roids[i]->get_size());
                        if(this->type == 0){
                            score = score + 10*roids[i]->get_initpower();
                        }
                        else if (this->type == 1){
                            score = score + 20*roids[i]->get_initpower();
                        }
                    }
                }
            } else
            if(this->type == 2){
                if ((this->x + this->size - 1) > (roids[i]->get_x() - roids[i]->get_size()) &&
                    (this->x - this->size + 1) < (roids[i]->get_x() + roids[i]->get_size()) &&
                    (this->y + this->size - 1) > (roids[i]->get_y() - roids[i]->get_size()) &&
                    (this->y - this->size + 1) < (roids[i]->get_y() + roids[i]->get_size())  ) {
                    play_sample(hit_roid, 255, 128, 1000, 0);

                    roids[i]->set_power(0);
                    if(roids[i]->get_power() <= 0) {                                                             //ASTEROID DESTROYED BY BULLET
                        if(roids[i]->get_powerup()){
                            pow->set_exists(true);
                            pow->set_type       (roids[i]->get_powerup());
                            pow->set_x          (roids[i]->get_x());
                            pow->set_y          (roids[i]->get_y());
                            pow->set_xtheta     (roids[i]->get_xtheta());
                            pow->set_ytheta     (roids[i]->get_ytheta());
                            pow->set_speed      (roids[i]->get_speed());
                            pow->set_distance   (roids[i]->get_distance());
                        }
                        roids[i]->set_powerup(0);
                        play_sample(splode_sound, 255, 128, 1000, 0);
                        explode_gfx(this->x, this->y, roids[i]->get_size());
                        score = score + 10*roids[i]->get_initpower();
                    }
                }
            }
        }
    }
}

void projectile::draw(BITMAP* buffer, int i){
    int color;

    int x2 = int(((this->distance - this->size) * cos(this->theta)) + SCREEN_W/2);
    int y2 = int(((this->distance - this->size) * sin(this->theta)) + SCREEN_H/2);

    if(this->power == 1)
        color = RED;
    else if(this->power == 2)
        color = YELLOW;
    else if(this->power == 3)
        color = GREEN;
    else
        color = WHITE;
    if(i == 0 || i == 1){
        this->set_size(int(SCREEN_W*.0035));
        circle(buffer, this->get_x(), this->get_y(), this->get_size(), color); //DRAW BULLET
    }
    else if(i == 2){
        line(buffer, this->get_x(), this->get_y(), x2, y2, GREEN);
    }
}

void explode_gfx(int x, int y, int range){
    int num_of_particles = (rand()%25)+5;
    for (int i = 0; i<num_of_particles; i++){
        int particle_x = (rand()%range);
        int particle_y = (rand()%range);
        int c = rand()%3;
        int color;
        int side = rand()%2;
        int sz = (rand()%5)+1;
        float delta = (float)ticks / (float)60;

        switch(c){
            case 0:
                color = RED;
                break;
            case 1:
                color = ORANGE;
                break;
            case 2:
                color = YELLOW;
                break;
        }

        if (side == 0){
            circle(buffer, (x-particle_x)+rand()%20, (y-particle_y)+rand()%20, sz, color);

        }
        else if (side == 1){
            circle(buffer, (x+particle_x)+rand()%20, (y+particle_y)+rand()%20, sz, color);
        }
    }
}
